home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / util / misc / Fudgit233.lha / Source / src / extramath.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-16  |  2.1 KB  |  124 lines

  1. #include  <math.h>
  2.  
  3. extern int errno;
  4.  
  5. /* This math function file is to make up for deficiencies in some
  6.  * library math functions.  The following are missing for some vendors:
  7.  *
  8.  *   (double) asinh( double )   inverse hyperbolic sin
  9.  *   (double) acosh( double )   inverse hyperbolic cos
  10.  *   (double) atanh( double )   inverse hyperbolic tan
  11.  *   (double) cbrt(  double )   cubic root
  12.  *   (double) rint(  double )   nearest rounded integer
  13.  *   (double) trunc( double )   truncated integer
  14.  *
  15.  * For Linux, it's rint, trunc, cbrt, gamma.
  16.  *
  17.  * Some versions of SunOS lack trunc().
  18.  */
  19.  
  20. #ifdef AMIGA
  21. double gamma(double x) {
  22.     return exp(lgamma(x));
  23. }
  24. #endif
  25.  
  26. #ifdef NOGAMMA
  27. double gamma(double x)
  28. {
  29.     double const cof[] = { 76.18009173E0, -86.50532033E0, 24.01409822E0,
  30.               -1.231739516E0, 0.120858003E-2, -0,536382E-5 } ;
  31.     double const stp = 2.50662827465, fpf = 5.5, pi=3.1415926535897932;
  32.  
  33.     int i;
  34.     double tmp,ser;
  35.  
  36. printf("gamma(%f) = ", x);
  37.     if (x < 1.)
  38.     return log(pi/sin(pi*(1.-x))) - gamma(1.-x);
  39.     else
  40.     {
  41.     x = x - 1.0;
  42.     tmp = x + fpf;
  43.     tmp = (x+.5)*log(tmp)-tmp;
  44.     ser = 1.0;
  45.     for (i=0; i<6; i++)
  46.     {
  47.         x = x + 1.0;
  48.         ser += cof[i]/x;
  49.     }
  50. printf("%f\n", tmp+log(stp*ser));
  51.     return tmp+log(stp*ser);
  52.     }
  53. }
  54. #endif
  55.  
  56. #ifdef NORINT
  57. double rint(double x)
  58. {
  59.      double diff;
  60.  
  61.      diff = x - floor(x);
  62.      if (diff >= 0.5) {
  63.          return(ceil(x));
  64.      }
  65.      else {
  66.            return(floor(x));
  67.      }
  68. }
  69. #endif
  70.  
  71. #ifdef NOTRUNC
  72. double trunc(double x)
  73. {
  74.     if (x < 0)
  75.         return(ceil(x));
  76.     else
  77.         return(floor(x));
  78. }
  79. #endif
  80.  
  81. #ifdef NOCBRT
  82. #define  ONE_THIRD 0.3333333333333333333333333333
  83. double cbrt(double x)
  84. {
  85.     double y, val, pow();
  86.  
  87.     y = fabs(x);
  88.     val = pow(y, ONE_THIRD) * (x/y);
  89.     return(val);
  90. }
  91. #endif
  92.  
  93. #ifdef NOHYP_TRIGO
  94. double acosh(double x)
  95. {
  96.     double val;
  97.  
  98.     val = log(x) + log(1.0 + sqrt(1.0 - 1.0/(x*x)));
  99.     return(val);
  100. }
  101.  
  102. double asinh(double x)
  103. {
  104.     double val;
  105.  
  106.     if(x == 0.0)
  107.         return(0.0);
  108.     val = log(fabs(x)) + log(1.0 + sqrt(1.0 + 1.0/(x*x)));
  109.     if(x < 0)
  110.         val = -val;
  111.     return(val);
  112. }
  113.  
  114. double atanh(double x)
  115. {
  116.     double val;
  117.  
  118.     val = 0.5 * log((1.0 + x)/(1.0 - x));
  119.     return(val);
  120. }
  121.  
  122. #endif
  123.  
  124.